﻿using System.Collections.Generic;
using SdmxMl.Common;
using SdmxMl.Structure;
using CarlosAg.ExcelXmlWriter;

namespace SdmxMl.Excel
{
    /// <summary>
    /// Specialized class for generating Excel export of ConceptScheme 
    /// </summary>
    public class ExcelExportConceptScheme : ExcelExportFlatHierarchy
    {
        #region C'tor

        private ConceptSchemeType cst;

        public ExcelExportConceptScheme(ConceptSchemeType c)
        {
            cst = c;
        }
        #endregion

        #region Header

        private void GenerateHeaderNames()
        {
            headerColitems = new List<string>(16);

            headerColitems.Add("Concept");
            headerColitems.Add("Parent");
            headerColitems.Add("Level");

            foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                headerColitems.Add("Name_" + lg);

            foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                headerColitems.Add("Desc_" + lg);

            headerColitems.Add("Codelist");
            headerColitems.Add("Text Format");
            headerColitems.Add("TF_Minlen");
            headerColitems.Add("TF_Maxlen");
            headerColitems.Add("TF_StartVal");
            headerColitems.Add("TF_EndVal");
            headerColitems.Add("TF_Decimal");
            
            anInventory = new AnnotationInventory();
            foreach (ConceptType c in cst.ConceptList)
                anInventory.Inventory(c.AnnotationList, false);

            anInventory.GenerateAnnotationHeader(headerColitems);
        }
        #endregion

        #region Overriden Export Method

        public override void Export()
        {
            GenerateBook("SMS", "OFS");
            GenerateWorksheetArtefact(cst);
            GenerateHeaderNames();

            sheet = GenerateWorksheetHeader(
                headerColitems.ToArray(), "Concepts", cst.ConceptList.Count);

            CreateRows(cst.ConceptList);
        }

        protected override MultiLang GetDescription(object ob)
        {
            MultiLang ml = base.GetDescription(ob);

            ConceptType c = ob as ConceptType;
            if (c != null)
                ml = c.Description;
            return ml;
        }

        /// <summary>
        /// In order to complete more detailled info like in concept
        /// </summary>
        /// <param name="row"></param>
        /// <param name="item"></param>
        protected override void FillAdditional(WorksheetRow row, ISdmxHierarchyListItem item)
        {
            // Codelist Ref (from dim or via concept)
            string CodelistRef = string.Empty;
            ConceptType c = item as ConceptType;
            if (c != null && c.CoreRepresentation.IsFilled)
                    CodelistRef = c.CoreRepresentation.FullIdent;
            
            AddCell(row, CodelistRef);

            // Text format
            TextFormatType tft = c != null ? c.TextFormat : new TextFormatType();
            AddCell(row, tft.TextType != TextTypeType.Undefined ? tft.TextType.ToString() : string.Empty);
            AddCell(row, tft.minLength);
            AddCell(row, tft.maxLength);
            AddCell(row, tft.IsNumeric ? tft.startValue : string.Empty);
            AddCell(row, tft.IsNumeric ? tft.endValue : string.Empty);
            AddCell(row, tft.IsNumeric ? tft.decimals : string.Empty);
        }

        #endregion
    }
}
